package edu.hawaii.duedates.webApp;

import java.util.Calendar;
import java.util.Timer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.duedates.DueDateTimer;
import edu.hawaii.duedates.DueDates;

/**
 * The java implementation of the Alerts page of the DueDates
 * application. This page enables the user to specify if they want 
 * an email sent to them once a day if there are any books due within 
 * a certain interval of time. Class extends WebPage.
 * 
 * @author Tyler Wolff, Mari-Lee Flestado, Aric West
 */
public class AlertsPage extends DueDatesPage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Constructs the Alerts page of the DueDates web application.
   * Page contains options to enable or disable alerts, specify what
   * libraries should be monitored, as well as Specify how close in 
   * time the due date of a book must be before generating the alert email.
   * 
   */
  public AlertsPage () {
    
    Form form = new Form("alertsForm");
    
    form.add(new CheckBox("enableAlerts", new PropertyModel(getSession(), "alertsEnabled")));
    
    form.add(new CheckBox("enableUhAlerts", new PropertyModel(getSession(), "enableUhAlerts")) {
      
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has UH library credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasUh();
      }
    });
    form.add(new CheckBox("enableHslAlerts", new PropertyModel(getSession(), "enableHslAlerts")) {
      
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has Hsl library credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasHsl();
      }
    });
    
    form.add(new RequiredTextField("daysUntilDue", 
        new PropertyModel(getSession(), "daysUntilDue")));
    
    
    form.add(new Button("updateButton") {
      private static final long serialVersionUID = 1L;
    
      /** Updates alerts information. */
      @Override
      public void onSubmit() {
        if (getDueDatesSession().isAlertsEnabled()) {
          String alertsString = "";
          if (getDueDatesSession().getEnableUhAlerts()) {
            alertsString += (getDueDatesSession().getUser().getUh() + " "); 
          }
          if (getDueDatesSession().getEnableHslAlerts()) {
            alertsString += (getDueDatesSession().getUser().getHsl() + " ");
          }
          alertsString += "-within " + getDueDatesSession().getDaysUntilDue() + " -email " 
              + getDueDatesSession().getEmail() + " " + getDueDatesSession().getSmtpServer() +  
              " -wakeup 1";
              
          String[] alertsArgs = alertsString.split(" ");
          DueDates alerts = new DueDates(alertsArgs);
          alerts.setItemsDue(alerts.getItems("all"));
          Timer timer = new Timer();
          Calendar date = Calendar.getInstance();
          //for outputs after the first
          DueDateTimer dueDateTimer = new DueDateTimer(alerts);
          timer.scheduleAtFixedRate(dueDateTimer, date.getTime(), 
              Long.valueOf(Math.round(1000 * 3600 * 24 * alerts.getWakeupValue())));

          getDueDatesSession().setAlertResponse("Alerts started Successfully");
          setResponsePage(AlertsPage.class);
        }
      }
    });
    
    form.add(new Label("response", getDueDatesSession().getAlertResponse()));
    form.add(new FeedbackPanel("feedback"));
    add(form);

    // adds links for navigation menu
    add(new Link("Results") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        setResponsePage(ResultPage.class);
      }
    });
    
    add(new Link("Alerts") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        setResponsePage(AlertsPage.class);
      }
    });
    
    add(new Link("Logout") {
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        getDueDatesSession().invalidate();
        setResponsePage(Index.class);
      }
    });
  }
}